package com.yami.shop.consign.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.vo.MarketAnalysisVO;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.consign.common.model.ConsignOptional;
import com.yami.shop.consign.common.service.ConsignAnalysisService;
import com.yami.shop.consign.common.service.ConsignOptionalService;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/p/app/consign/optional")
@Tag(name = "寄售自选（c端）")
@Slf4j
@AllArgsConstructor
public class ConsignOptionalController {

    private final ConsignOptionalService consignOptionalService;

    private final ConsignAnalysisService consignAnalysisService;

    @GetMapping("/{prodId}")
    @Operation(summary =  "添加、取消自选", description = "添加、取消自选")
    public ServerResponseEntity<Void> updateOptional(@PathVariable("prodId") Integer prodId) {
        YamiUser user = SecurityUtils.getUser();
        consignOptionalService.addOrCancel(prodId, user.getUserId());
        return ServerResponseEntity.success();
    }

    @GetMapping("/whether")
    @Operation(summary =  "是否自选", description = "是否自选")
    public ServerResponseEntity<Boolean> isOptional(@RequestParam Integer prodId) {
        YamiUser user = SecurityUtils.getUser();
        Boolean whether = false;
        ConsignOptional one = consignOptionalService.getOne(new LambdaQueryWrapper<ConsignOptional>().eq(ConsignOptional::getProdId, prodId).eq(ConsignOptional::getUserId, user.getUserId()));
        if (Objects.nonNull(one)) {
            whether = true;
        }
        return ServerResponseEntity.success(whether);
    }


    @GetMapping("/analysis/page")
    @Operation(summary =  "自选市场分析分页列表", description = "自选市场分析分页列表")
    public ServerResponseEntity<PageVO<MarketAnalysisVO>> page(@Valid PageDTO pageDTO, Integer sort, String prodName){
        YamiUser user = SecurityUtils.getUser();
        PageVO<MarketAnalysisVO> page = consignAnalysisService.getPage(pageDTO, sort, 2, user.getUserId(), prodName);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/analysis/upsDowns")
    @Operation(summary =  "涨跌数量统计", description = "涨跌数量统计")
    public ServerResponseEntity<Map<String, Integer>> getUpsDowns(){
        YamiUser user = SecurityUtils.getUser();
        Map<String, Integer> map = consignAnalysisService.upsDowns(user.getUserId());
        return ServerResponseEntity.success(map);
    }


}
